package com.qtenv.datasync.worker.degao;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qtenv.datasync.api.degao.DeGaoApi;
import com.qtenv.datasync.base.DataSyncer;
import com.qtenv.datasync.constant.AppConstants;
import com.qtenv.datasync.service.entity.TerminalHistoryData;
import com.qtenv.datasync.service.entity.Terminals_bf;
import com.qtenv.datasync.service.entity.degao.DGTerminalHistoryData;
import com.qtenv.datasync.service.service.TerminalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName TAXDataSyncWorker
 * @Author CBL
 * @Date 2022/2/24 14:52
 **/
@Component
public class DGDataSyncWorker extends DataSyncer {
    @Autowired
    private TerminalsService terminalsService;

    public DGDataSyncWorker() {
        super("德高");
    }

    @Override
    public List<TerminalHistoryData> getTerminalHistoryData(String tenantId, String strStartTime, String strEndTime) {
        List<Terminals_bf> terminalsList = terminalsService.findMasTerminalsByTenantAndSupplier(tenantId, AppConstants.DG_SUPPLIER_ID);
//        terminalsList.removeIf(t-> !t.getTerminalID().equals("9112"));
        String tids  = terminalsList.stream().filter(item -> item.getSupplierID() == 1002).map(Terminals_bf::getTerminalID).collect(Collectors.joining(","));

        return this.doGetTerminalHistoryData(tenantId, tids, strStartTime, strEndTime);

    }

    @Override
    public List<TerminalHistoryData> getTerminalHistoryDataForTerminal(Terminals_bf terminals, String tenantId, String strStartTime, String strEndTime) {
        return super.getTerminalHistoryDataForTerminal(terminals, tenantId, strStartTime, strEndTime);
    }

    @Override
    public List<TerminalHistoryData> getTerminalHistoryData(String tenantId, List<String> terminalIdList, String strStartTime, String strEndTime) {
        String tids = String.join(",", terminalIdList);
        return this.doGetTerminalHistoryData(tenantId, tids, strStartTime, strEndTime);
    }

    public List<TerminalHistoryData> doGetTerminalHistoryData(String tenantId, String tids, String strStartTime, String strEndTime) {
        LocalDate startDate = LocalDate.parse(strStartTime.substring(0, 10));
        String strBegin = startDate.toString() + "T00:00:00";

        LocalDate endDate = LocalDate.parse(strEndTime.substring(0, 10));
        String strEnd = endDate.toString() + "T23:59:59";

        String dataStr = DeGaoApi.GetScadaValueByUserTerminals(tids, strBegin, strEnd);
        List<TerminalHistoryData> dataEntities = this.transform(tenantId, dataStr);
        if (CollectionUtil.isEmpty(dataEntities)) {
            System.out.println(String.format("大表【%s】当天暂无数据需要同步", tids));
        } else {
            return dataEntities;
        }

        return null;
    }

    private List<TerminalHistoryData> transform(String tenantId, String strData) {
        List<TerminalHistoryData> result = CollectionUtil.newArrayList();

        JSONArray jsonArray = JSON.parseArray(strData);
        if (CollectionUtil.isNotEmpty(jsonArray)) {
            jsonArray.forEach(json -> {
                JSONObject jsonObject = JSON.parseObject(json.toString());
                String terminalID = jsonObject.getString("Id");
                JSONArray dataArray = JSON.parseArray(jsonObject.getString("Data"));
                dataArray.forEach(dataJson -> {
                    JSONObject dataJsonObject = JSON.parseObject(dataJson.toString());
                    Date readDate = DateUtil.parse(dataJsonObject.getString("Time"), DatePattern.NORM_DATETIME_FORMAT);
                    DGTerminalHistoryData dgTerminalHistoryData = JSON.parseObject(dataJsonObject.getString("Value"), DGTerminalHistoryData.class);

                    TerminalHistoryData terminalHistoryData = new TerminalHistoryData();
                    BeanUtil.copyProperties(dgTerminalHistoryData, terminalHistoryData);
                    terminalHistoryData.setTenant_id(tenantId);
                    terminalHistoryData.setSupplierID(AppConstants.DG_SUPPLIER_ID);
                    terminalHistoryData.setTerminalID(terminalID);
                    terminalHistoryData.setReadDate(readDate);
                    terminalHistoryData.setReadDate_s(readDate);
                    result.add(terminalHistoryData);
                });
            });
        }

        return result;
    }

    @Override
    public List<TerminalHistoryData> doGetTerminalHistoryDataFromTerminal(Terminals_bf terminals, String tenantId, String tids, String strStartTime, String strEndTime) {
        return super.doGetTerminalHistoryDataFromTerminal(terminals, tenantId, tids, strStartTime, strEndTime);
    }

    @Override
    public List<TerminalHistoryData> transformFromTerminal(Terminals_bf terminals, String tenantId, String strData) {
        return super.transformFromTerminal(terminals, tenantId, strData);
    }


    public static void main(String[] args) {
        DGDataSyncWorker worker = new DGDataSyncWorker();
//        List<TerminalHistoryData> historyDataList = worker.getTerminalHistoryData("2022-03-17", "2022-03-17");
//        System.out.println("size=" + historyDataList.size());
//        System.out.println(JSON.toJSONString(historyDataList, true));

        // 德高数据接口拉取当天数据-有数据

//        String sp = " 00:00:00";
//        LocalDate s = LocalDate.parse("2022-06-01");
//        String ep = " 23:59:59";
//        LocalDate e = LocalDate.parse("2022-06-01");
//        while (s.isBefore(e) || s.isEqual(e)) {
//            worker.syncTerminalHistoryData(s.toString() + sp, s.toString() + ep);
//            s = s.plusDays(1L);
//        }
    }
}
